/**
 * @Title: ChangeVehicleAdapter.java
 * @Description: TODO
 * @author 7828wc
 * @date 2015年8月6日 下午5:43:01
 */
package com.shanghaionstar.user.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.auth.userProfile.Vehicle;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;

import java.util.List;

/**
 * @ClassName: ChangeVehicleAdapter
 * @Description: 切换车辆适配器
 * @author 7828wc
 * @date 2015年8月6日 下午5:43:01
 */
public class ChangeVehicleAdapter extends CommonAdapter<Vehicle> {
    Context mContext;
    private List<Vehicle> vehicleList;
    private int position = -1;
    private String vin;
    private OnItemClickListener onItemClick;

    /**
     * @author 7828wc
     * @date 2015年8月7日 上午11:08:40
     */
    public ChangeVehicleAdapter(List<Vehicle> datas, Context context) {
        super(datas, context);
        mContext = context;
        vehicleList = datas;
    }

    /**
     * (non-Javadoc)
     * <p>
     * Title: getView
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @param position
     * @param viewHolder
     * @see CommonAdapter#getView(int,
     *      CommonViewHolder)
     */
    @Override
    public void getView(final int position, CommonViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder) viewHolder;
        Vehicle vehicleInfo = vehicleList.get(position);
        if (vin != null && vin.equals(vehicleInfo.getVin())) {
            this.position = position;
        }

        if (this.position == position) {
            holder.vehicle_selected.setVisibility(View.VISIBLE);
        } else {
            holder.vehicle_selected.setVisibility(View.GONE);
        }

        if (!CommonUtil.isNull(vehicleInfo.getMakeDesc()) && !CommonUtil.isNull(vehicleInfo.getModelDesc())) {
            holder.vehicle_name.setText(vehicleInfo.getMakeDesc() + vehicleInfo.getModelDesc());
            holder.vehicle_name.setVisibility(View.VISIBLE);
        } else {
            holder.vehicle_name.setVisibility(View.GONE);
        }
        holder.vehicle_vin.setText("VIN: " + CommonUtil.maskWithStar(vehicleInfo.getVin(), 3, 4));
        holder.vehicle_item.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null) {
                    onItemClick.onItemClick(position);
                }
            }
        });

    }

    public void setOnItemClick(OnItemClickListener onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClickListener {
        void onItemClick(int arg0);
    }

    public void setSelectVin(String vin) {
        this.vin = vin;
    }

    /**
     * (non-Javadoc)
     * <p>
     * Title: layoutId
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @return
     * @see CommonAdapter#layoutId()
     */
    @Override
    public int layoutId() {
        return R.layout.more_change_vehicle_item;
    }

    /**
     * (non-Javadoc)
     * <p>
     * Title: getViewHolder
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @return
     * @see CommonAdapter#getViewHolder()
     */
    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    private class ViewHolder extends CommonViewHolder {
        @FindViewById
        private TextView vehicle_name, vehicle_vin;
        @FindViewById
        private CustomImageView vehicle_selected;
        @FindViewById
        private RelativeLayout vehicle_item;
    }

}
